package com.mystudy.springcloud.controller;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.mystudy.springcloud.controller.service.PaymentOpenFeignClientService;
import com.mystudy.springcloud.entities.CommonResult;
import com.mystudy.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class CircleBreakerController {
	@Autowired
	private RestTemplate restTemplate;

	@Value("${service-url.nacos-user-service}")
	public String serverURL;

	@GetMapping("/consumer/fallback/{id}")
	// @SentinelResource(value = "fallback") // 没有配置
	// @SentinelResource(value = "fallback", fallback = "handlerFallback") // fallback只负责业务异常
	// @SentinelResource(value = "fallback", blockHandler = "blockHandler") // blockHandler只负责控制台配置违规
	@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler", exceptionsToIgnore = {IllegalArgumentException.class})
	public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
		CommonResult<Payment> result = restTemplate.getForObject(serverURL + "/paymentSQL/" + id, CommonResult.class, id);
		if (id == 4) {
			throw new IllegalArgumentException("IllegalArgumentException，非法参数异常....");
		} else if (result.getData() == null) {
			throw new NullPointerException("NullPointerException，该ID没有对应记录,空指针异常");
		}
		return result;
	}

	/**
	 * @功能: fallback的兜底方法
	 * @作者: 高志红
	 */
	public CommonResult<Payment> handlerFallback(@PathVariable("id") Long id, Throwable e) {
		Payment payment = new Payment(id, "这个人太过于神秘！");
		return new CommonResult<>(444, "fallback的兜底方法，异常内容：" + e.getMessage(), payment);
	}

	/**
	 * @功能: blockHandler的兜底方法
	 * @作者: 高志红
	 */
	public CommonResult<Payment> blockHandler(@PathVariable("id") Long id, BlockException e) {
		Payment payment = new Payment(id, "这个人太牛了！");
		return new CommonResult<>(445, "blockHandler的兜底方法，异常内容：" + e.getMessage(), payment);
	}

	@Autowired
	private PaymentOpenFeignClientService paymentOpenFeignClientService;

	/**
	 * @功能: 测试Sentimel集成OpenFeign
	 * @作者: 高志红
	 */
	@GetMapping("/consumer/openfeign/{id}")
	@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler")
	public CommonResult<Payment> openFeign(@PathVariable("id") Long id) {
		CommonResult<Payment> result = paymentOpenFeignClientService.paymentSQL(id);
		if (id == 4) {
			throw new IllegalArgumentException("IllegalArgumentException，非法参数异常....");
		} else if (result.getData() == null) {
			throw new NullPointerException("NullPointerException，该ID没有对应记录,空指针异常");
		}
		return result;
	}
}
